import java.util.ArrayList;
import java.util.HashMap;


public class Parser {
	
	
	public static HashMap<String, UserBreakdown> from_mbid_to_userBreakdown = new HashMap<String, UserBreakdown>();

	
	public Parser () {
		parse_nationalities("states.txt");
		setContinentsColors();
		parse_artists_users("typicalUsers.txt");
		//check();
	}
	
	
	// Parse the center of each US state
	public void parse_nationalities( String fileName ) {
		
		String[] lines = GLOBAL.processing.loadStrings(fileName);
        
        for(int i = 0; i < lines.length; i++) {
        	
            // split the row on the tabs
            String[] pieces = GLOBAL.processing.split(lines[i], GLOBAL.processing.TAB);
            
            if(pieces.length<5) { continue; }

            float latitude =0, longitude =0;
            latitude= Float.valueOf(pieces[0]);
            longitude= Float.valueOf(pieces[1]);
            String continentID = pieces[3];
            
            boolean found = false;
            for (int j=0; j<GLOBAL.allContinents.size(); j++) {
            	
            	// If the continent already exists 
            	if (GLOBAL.allContinents.get(j).name.equals(continentID)) {
            		found = true;
            		// Insert new nationality
                    Nationality n = new Nationality(pieces[2],latitude,longitude,GLOBAL.allContinents.get(j));
                    GLOBAL.allNationalities.add(n);
                    GLOBAL.map_allNationalities.put(pieces[2], n);
            		break;
            	}
            }
            
            // If the continent not exists
            if (!found) {
            	// Insert new nationality
            	Continent c = new Continent(continentID);
                Nationality n = new Nationality(pieces[2],latitude,longitude,c);
                GLOBAL.allNationalities.add(n);
                GLOBAL.map_allNationalities.put(pieces[2], n);
                GLOBAL.allContinents.add(c);
            }
     
        }
	}
	
	public void setContinentsColors() {
		for (int i=0; i< GLOBAL.allContinents.size();i++) {
			GLOBAL.allContinents.get(i).color = GLOBAL.COLORS.getColor(i);
		}
	}
	
	public void parse_artists_users( String fileName ) {
		
		String[] lines = GLOBAL.processing.loadStrings(fileName);

        for(int i = 0; i < lines.length; i++) {
        	
//        	System.out.println(i);
        	
            // split the row on the tabs
            String[] pieces = GLOBAL.processing.split(lines[i], GLOBAL.processing.TAB);
            
        	if (pieces.length < 2) continue;
            
        	String user_mbid = pieces[0]; // MusicBrainz ID
    		ArrayList<String> user_nationalities = new ArrayList<String>();
    		ArrayList<Integer> user_ages_integers = new ArrayList<Integer>();
    		ArrayList<String> user_ages_intervals = new ArrayList<String>();
    		String user_sex = "";

            // For every 
    		boolean nat_finished = false;
            for(int j=1; j<pieces.length; j++) {
            	if (isNumeric(pieces[j]) || pieces[j].length() == 1)
            		nat_finished = true; // No more nationalities
            	
            	if (!nat_finished) { // Add nationality
            		user_nationalities.add(pieces[j]);
            		continue;
            	}
            	
            	else {
            		if (isNumeric(pieces[j])) {
            			user_ages_integers.add(Integer.parseInt(pieces[j]));
            			continue;
            		}
            		else {
            			user_sex = pieces[j]; // save sex
            			break; // should be the last one
            		}
            	}
            	
            }
            
			//user_ages_intervals = setIntervals(user_ages_integers);

            // Add new data structure for this artist
            from_mbid_to_userBreakdown.put(user_mbid, 
            		new UserBreakdown(user_nationalities, user_ages_integers, 
            				user_ages_intervals, user_sex));
        	
        }
	}
	
	public static boolean isNumeric(String str)  
	  {  
	    try  
	    {  
	      double d = Double.parseDouble(str);  
	    }  
	    catch(NumberFormatException nfe)  
	    {  
	      return false;  
	    }  
	    return true;  
	  }  
	
	
	public void check() {
		for(int i=0; i<GLOBAL.allNationalities.size(); i++) {
			System.out.println(GLOBAL.allNationalities.get(i).continent.color);
		}
	}
	
	
	// Parsed all artists user breakdown
	public class UserBreakdown{
		
		public ArrayList<String> user_nationalities = new ArrayList<String>();
		public ArrayList<Integer> user_ages_integers = new ArrayList<Integer>();
		public ArrayList<String> user_ages_intervals = new ArrayList<String>();
		public String user_sex = "";
		
		public UserBreakdown(ArrayList<String> user_nationalities, ArrayList<Integer> user_ages_integers,
				ArrayList<String> user_ages_intervals,String user_sex) {
			
			this.user_nationalities = user_nationalities;
			this.user_ages_integers = user_ages_integers;
			this.user_ages_intervals = user_ages_intervals;
			this.user_sex = user_sex;
			
		}
		
	}
}
